import Vue from 'vue'
import VueRouter from 'vue-router'
// import login from '../views/login/index'
// import layout from '../views/layout/index'
// import welcome from '@/views/layout/welcome'
// import chart from '@/views/layout/chart'
// import user from '@/views/layout/user'
// import enterprise from '@/views/layout/enterprise'
// import question from '@/views/layout/question'
// import subject from '@/views/layout/subject'
import { getToken } from '@/utils/token.js'
import parent from '@/test/slot/parent'
import parent2 from '@/test/slot2/parent'
import parent3 from '@/test/slot3/parent'
Vue.use(VueRouter)

const login = () => import('../views/login/index')
const layout = () => import('../views/layout/index')
const welcome = () => import('@/views/layout/welcome')
const chart = () => import('@/views/layout/chart')
const user = () => import('@/views/layout/user')
const enterprise = () => import('@/views/layout/enterprise')
const question = () => import('@/views/layout/question')
const subject = () => import('@/views/layout/subject')

const routes = [
  {
    path: '/login',
    name: 'login',
    component: login
  },
  {
    path: '/',
    redirect: '/welcome'
  },
  {
    path: '/parent',
    component: parent
  },
  {
    path: '/parent2',
    component: parent2
  },
  {
    path: '/parent3',
    component: parent3
  },
  {
    path: '/layout',
    name: 'layout',
    component: layout,
    children: [
      {
        path: 'welcome',
        component: welcome,
        meta: {
          title: '欢迎页面', // 左侧的标题和页面标题
          icon: 'el-icon-pie-chart', // 图标
          path: '/layout/welcome', // 访问的路径
          roles: ['超级管理员', '管理员', '老师', '学生'] // 哪些角色可以访问
        }
      },
      {
        path: 'chart',
        component: chart,
        meta: {
          title: '数据预览', // 左侧的标题和页面标题
          icon: 'el-icon-pie-chart', // 图标
          path: '/layout/chart', // 访问的路径
          roles: ['超级管理员', '管理员', '老师'] // 哪些角色可以访问
        }
      },
      {
        path: 'user',
        component: user,
        meta: {
          title: '用户列表', // 左侧的标题和页面标题
          icon: 'el-icon-user', // 图标
          path: '/layout/user', // 访问的路径
          roles: ['超级管理员', '管理员', '老师'] // 哪些角色可以访问
        }
      },
      {
        path: 'enterprise',
        component: enterprise,
        meta: {
          title: '企业列表', // 左侧的标题和页面标题
          icon: 'el-icon-office-building', // 图标
          path: '/layout/enterprise', // 访问的路径
          roles: ['超级管理员', '管理员', '老师'] // 哪些角色可以访问
        }
      },
      {
        path: 'question',
        component: question,
        meta: {
          title: '题库列表', // 左侧的标题和页面标题
          icon: 'el-icon-edit-outline', // 图标
          path: '/layout/question', // 访问的路径
          roles: ['超级管理员', '管理员', '老师', '学生'] // 哪些角色可以访问
        }
      },
      {
        path: 'subject',
        component: subject,
        meta: {
          title: '学科列表', // 左侧的标题和页面标题
          icon: 'el-icon-notebook-2', // 图标
          path: '/layout/subject', // 访问的路径
          roles: ['超级管理员', '管理员', '老师'] // 哪些角色可以访问
        }
      }
    ]
  }
]

const router = new VueRouter({
  routes
})
const whiteList = ['/login']
router.beforeEach((to, from, next) => {
  if (whiteList.includes(to.path)) {
    next()
  } else {
    if (getToken()) {
      next()
    } else {
      next('/login')
    }
  }
})

// 后置守卫(当你真正进入到某个页面之后才执行)
router.afterEach((to, from) => {
  document.title = to.meta.title || '黑马面面'
})
export default router
